﻿using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using IPMS.Sort;
using IPMS.Common;
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using System.Web;
using SysOperationLayer.DBHandle;
using System.Data;
using SysComm;

public partial class SysResetPasswd : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        if (Session["Culture"] != null)
        {
            strCulture = Session["Culture"].ToString();
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }

    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorUsertype())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }

        if (Session[SysUtil.m_str_session_loginname] == null)
        {
            Response.Redirect("SysLogin.aspx");
        }
        else
        {
            if (!Page.IsPostBack)
            {
                string strLoginName = Request["userid"];
                txtLoginName.Text = Session[SysUtil.m_str_session_loginname].ToString();

                if (strLoginName != null)
                {
                    UsersDB objDB = new UsersDB();
                    DataRow dr = objDB.GetItem(strLoginName);

                    if (dr != null)
                    {
                        txtLoginName.Text = objDB.GetValue(dr, "LoginName");
                    }
                }

                txtNewPassword.Text = "";
                txtRetypeNewPassword.Text = "";
            }
        }
    }

    protected void OnClick(object sender, EventArgs e)
    {
        if (sender == btnSave)
        {
            lblError.Text = "";

            if (txtNewPassword.Text == txtRetypeNewPassword.Text)
            {
                UsersDB objDB = new UsersDB();

                if (objDB.ResetPassword(txtLoginName.Text.Trim(), txtNewPassword.Text.Trim()) == 0)
                {
                    lblError.Text = "Reset User Password Success!";
                    new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_user_manage, "Reset User Password Success!");
                    txtNewPassword.Text = "";
                    txtRetypeNewPassword.Text = "";
                }
                else
                {
                    lblError.Text = "Reset User Password Failure!" + objDB.ErrorMessage;
                    new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_user_manage, "Reset User Password Failure!");
                }

                objDB.CloseDB();
            }
            else
            {
                lblError.Text = "New Password and Retype New Password Not Match!";
            }
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("SysUsers.aspx");
    }
}
